package com.jojo.android.zxlib.base;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.jojo.android.zxlib.R;
import com.jojo.android.zxlib.tool.Global;
import com.jojo.android.zxlib.tool.ZxAppTool;
import com.jojo.android.zxlib.tool.ZxDBHelper;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxTool;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * 基础Application
 *
 * @author ZXW
 * @use extends BaseApplication 或 在你的Application的onCreate方法中BaseApplication.init(this);
 * @see #init
 */
public abstract class BaseApplication extends Application {
    private static final String TAG = "BaseApplication";

    public BaseApplication() {
    }

    private static Application instance;

    public static Application getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ZxLogTool.d(TAG, "项目启动 >>>>>>>>>>>>>>>>>>>> \n\n");

        init(this);
    }

    /**
     * 初始化方法
     *
     * @param application
     * @must 调用init方法且只能调用一次，如果extends BaseApplication会自动调用
     */
    private void init(Application application) {
        instance = application;
        if (instance == null) {
            ZxLogTool.e(TAG, "\n\n\n\n\n !!!!!! 调用BaseApplication中的init方法，instance不能为null !!!" +
                    "\n <<<<<< init  instance == null ！！！ >>>>>>>> \n\n\n\n");
        }
        Global.init(instance);  //初始化全局变量
        ZxTool.init(instance);  //初始化常用工作
        ZxLogTool.init(instance); //初始化日志工具
        initBugly(getBuglyId()); //初始化崩溃捕获
        ZxDBHelper.init(instance,getDbName()); //初始化数据库工具
    }

    protected String getDbName(){
        return null;
    }

    /**
     * 为了实现崩溃上报，必须实现该方法，
     * 提供在腾讯平台注册的BuglyId
     * @return
     */
    protected abstract String getBuglyId();



    private void initBugly(String buglyId) {
        if (TextUtils.isEmpty(buglyId)){
            return;
        }
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, buglyId, true);
    }


    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取应用名
     *
     * @return
     */
    public String getAppName() {
        return getResources().getString(R.string.app_name);
    }

    /**
     * 获取应用版本名(显示给用户看的)
     *
     * @return
     */
    public String getAppVersion() {
        return ZxAppTool.getAppVersionName(instance);
    }


}
